# encoding: utf-8
"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by siyi.chen on '30/12/2024'
    comment: 设备相关
"""
from fastapi import APIRouter, Request

from config import Config
from project.core.function import json_response
from project.lib.decorator import api_common, exec_duration
from project.micro_service.zeus import Zeus


device_router = APIRouter(prefix='/c', tags=['cariabot.device'])


@device_router.get('/device/relation/list', summary='设备关联列表')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def get_device_relation_list(request: Request, device=None):
    """
    **desc**: 设备关联列表（排除自身）

    **url**: http(s)://{0}.udicaria.com/c/device/relation/list

    **method**: GET

    **params**:

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: [
            {
                name: 设备名称
                code: 设备编号
                device_type: 设备类型
            }]
            exec_time: 接口后端处理时长
        }
    """
    ret_data = await Zeus.get_device_relation_list(device.dev_id)
    data = [v for v in ret_data if v['code'] != device.dev_id]

    return json_response(data=data)


@device_router.get('/device/connect', summary='设备连接')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def do_device_connect(request: Request, device=None):
    """
    **desc**: 设备连接服务器

    **url**: http(s)://{0}.udicaria.com/c/device/connect

    **method**: GET

    **params**:

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:
            exec_time: 接口后端处理时长
        }
    """
    await Zeus.do_device_connect(device.dev_id)

    return json_response()
